#===============================================================================
# Koro Business Environment
# Copyright (C) 2008  Antonio Corroppoli
#
#This file is part of Koro BE.
# 
# Koro BE is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Koro BE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Koro BE; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#===============================================================================

import pprint
import xml.dom.minidom
from xml.dom.minidom import Node


class gdom:    

    def __init__(self,className):
        self.className=className
        self.attrList=[]
        self.getters=[]
        self.setters=[]

    def addAttr(self,attr):
        self.attrList.append(attr)
        self.getters.append(self.createGetter(attr))
        self.setters.append(self.createSetter(attr))

    def createSetter(self,attr):
        setter="def set"+attr.capitalize()+"(self,val):\n"+8*' '+"self.__"+attr+" = val \n"
        return setter
        

    def createGetter(self,attr):
        getter="def get"+attr.capitalize()+"(self):\n"+8*' '+"return self.__"+attr+"\n"
        return getter

    def createClassCode(self):
        template="class %s(object):\n    def __init__(self):\n" % self.className
        for attr in self.attrList:
            template+=8*' '+"self.__"+attr+"=None \n"
        template+="\n"
        for getter in self.getters:
            template+="    "+getter+"\n"
        for setter in self.setters:
            template+="    "+setter+"\n"

        return template


def parse():
    code=""    
    doc = xml.dom.minidom.parse("gaptestdb.xml")
    for node in doc.getElementsByTagName("table"):
        d=gdom(node.getAttribute("name"))
        idnode = node.getElementsByTagName("id").item(0)
        d.addAttr(idnode.getAttribute("name"))
        for node2 in node.getElementsByTagName("column"):
            d.addAttr(node2.getAttribute("name"))
        for node3 in node.getElementsByTagName("fk"):
            d.addAttr(node3.getAttribute("name"))
        code+=d.createClassCode()
        code+="\n\n"
    return code

if __name__ == "__main__":
    print parse()
##    d=gdom("Prova")
##    d.addAttr("id")
##    d.addAttr("ragsoc")
##    d.addAttr("categ")
##    d.addAttr("piva")
##    d.addAttr("nomebreve")
##    d.createClassCode("pippo")
    
